<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <%- include ../shop/common.ejs%>
    <style>
        .thumb{
            width: 30%;
            /*background-image: url("http://123.57.72.99:3000/images/goods/5881b28b2f6e8730d409a251e83c9a0f.jpg");*/
            background-repeat: no-repeat;
            background-size: cover;
            height: 80px;
            width: 80px;
            margin: 3px;
        }
    </style>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-xs-12 form-inline" style="padding: 10px 0 10px 15px">
            <div class="form-group">
                <select id="dispatchid" class="form-control">
                <%dispatchs.forEach(function(dispatch){%>
                    <option value="<%=dispatch.id%>"><%=dispatch.name%></option>
                <%});%>
                </select>
            </div>
            <div class="form-group">
                <input type="text" id="dispatchNum" class="form-control" id="dispatchNum" placeholder="请输入订单编号">
            </div>
            <div class="form-group">
                <a href="#" onclick="dispatch()"><button type="button" class="btn btn-default">发货</button></a>&nbsp;&nbsp;
                <span style="color: red;">
                    提示：选中一条记录填写订单编号并发货
                </span>
            </div>
        </div>

        <div class="col-md-12">
            <table class="table table-striped" id="lotteryTable">
                <thead>
                <tr>
                    <th>√</th>
                    <th>#</th>
                    <th>商品名称</th>
                    <th>参与人次</th>
                    <th>中奖期数</th>
                    <th>中奖号码</th>
                    <th>收货地址</th>
                    <th>快递</th>
                    <th>物流单号</th>
                    <th>发货状态</th>
                    <th>开奖时间</th>
                </tr>
                </thead>
                <tbody>
                <%data.rows.forEach(function(orders,index){%>
                <tr>
                    <th><input name="lottery_id" type="checkbox" value="<%=orders.id%>"></th>
                    <th scope="row"><%=index+1%></th>
                    <td><%=orders.title%></td>
                    <td><%=orders.count%></td>
                    <td><%=orders.term%></td>
                    <td><%=orders.lottery%></td>
                    <td><%=orders.address%></td>
                    <td><%=orders.name%></td>
                    <td><%=orders.dispatchNum%></td>
                    <td>
                        <%if(orders.dispatchStatus=='no'){%>
                        未发货
                        <%}else{%>
                        已发货
                        <%}%>
                    </td>
                    <td><%=orders.createtime%></td>


                </tr>
                <%})%>


                </tbody>
            </table>
        </div>
        <div class="col-xs-12">
            <ul id="display-page"></ul>
        </div>
    </div>
</div>

</body>
<script>
/*
    $('#openModal').click(function(){
        $('#myModal').modal('toggle');
    });*/
    init(<%= data.total%>);//初始化分页
    function init(total){
//        alert(total);
        if(total==0){
            total=1;
        }
        var options = {
            currentPage: 1,
            totalPages: parseInt((Number(total)+10-1)/10,10),
            alignment:'center',
            bootstrapMajorVersion:3,
            tooltipTitles: function (type, page, current) {
                switch (type) {
                    case "first":
                        return "跳转到首页";
                    case "prev":
                        return "上一页";
                    case "next":
                        return "下一页";
                    case "last":
                        return "跳转到末页";
                    case "page":
                        return "转到 " + page;
                }
            },
            onPageClicked:function(event, originalEvent, type,page){
                reloadpage(page,false);
            }
        }

        $('#display-page').bootstrapPaginator(options);
    }
    function reloadpage(page,isinit){
        $.get("/lottery/list",
                {page:page,dispatch:'false'},
                function(result){
                    if(isinit){
                        init(result.total);
                    }

                    if(result.rows.length<1){
                        $('#rowcontent').html('无数据...');
                        return;
                    }
                    var html = '';
                    result.rows.forEach(function(orders,index){
                        var status = '已发货';
                        if(orders.dispatchStatus=='no'){
                            status = '未发货';
                        }
                        html+='<tr><th><input name="lottery_id" type="checkbox" value="'+orders.id+'"></th> <th scope="row">'+(index+1)+'</th>' +
                               '<td>'+orders.title+'</td>'+
                                '<td>'+orders.count+'</td>' +
                                '<td>'+orders.term+'</td>' +
                                '<td>'+orders.lottery+'</td>' +
                                '<td>'+orders.address+'</td>' +
                                '<td>'+orders.name+'</td>' +
                                '<td>'+orders.dispatchNum+'</td>' +
                                '<td>'+status+'</td>' +
                                '<td>'+orders.createtime+'</td>' +
                                '</tr>';
                    });
                    $('#lotteryTable tbody').html(html);
                });
    }

    //发货
    function dispatch(){
        var id = selectid();
        if(!id){
            return alert('请选择一条记录!');
        }
//        return alert(id);
        $.post('/lottery/update',{
            id:id,
            dispatchStatus:'yes',
            dispatchid:$('#dispatchid').val(),
            dispatchNum:$('#dispatchNum').val()
        },function(result){
            if(result.msg){
                location.reload(true);
            }else{
                alert('失败！');
            }
        });
    }

    function selectid(){
        var adIds = "";
        $("input:checkbox[name='lottery_id']:checked").each(function(i){
            if(0==i){
                adIds = $(this).val();

            }
        });
//        if(!adIds){
//            return alert('请选择一条记录!');
//        }
        return adIds;
    }
</script>
</html>